﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;

namespace WISD.Common
{
    /// <summary>
    /// Sql语句执行队列
    /// 
    /// </summary>
    public class SqlCommandExecuteQueue
    {
        private StringBuilder batchSqlSb = new StringBuilder();
        private int maxCommandCount;
        private int currentCount;
        private DbCommand cmd;
        public SqlCommandExecuteQueue(DbCommand cmd, int maxCommandCount = 100)
        {
            this.cmd = cmd;
            this.maxCommandCount = maxCommandCount;
        }

        public void AddCommand(string sqlCmd)
        {
            currentCount++;
            batchSqlSb.Append(sqlCmd);
            if (maxCommandCount == currentCount)
            {
                Console.WriteLine("达到{0}条", currentCount);
                ExecuteRemaining();
            }
        }

        public void ExecuteRemaining()
        {
            Console.WriteLine("执行");
            cmd.CommandText = batchSqlSb.ToString();
            cmd.ExecuteNonQuery();
            Console.WriteLine("完成");
            batchSqlSb.Clear();
            currentCount = 0;
        }

    }
}
